﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.ExtMethod
{
    public static class ControlFunction
    {

        /// <summary>
        ///     计算当前控件置于制定控件的垂直居中的top值
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="desControl"></param>
        /// <returns></returns>
        public static int GetVerticalMiddle(this Control ctrl, Control desControl)
        {
            if (ctrl == null)
            {
                return 0;
            }

            int temp = 0;

            if (ctrl.Width > desControl.Width)
            {
                temp = 0;
            }
            else
            {
                temp = desControl.ClientSize.Height / 2 - ctrl.Size.Height / 2;
            }

            //var temp = rect.Height/2 + ctrl.Size.Height;

            //如果不是父控件，则需要再加上控件所在位置的top
            if (ctrl.Parent != desControl)
            {
                var f = desControl.FindForm();
                if (f != null)
                {

                    Point p = default(Point);

                    desControl.Invoke(() =>
                    {
                        p = desControl.PointToScreen(new Point(0, 0));
                    });

                    temp += p.Y;
                }

            }

            return temp;

        }

        /// <summary>
        ///     计算当前控件置于制定控件的垂直居中的left值
        /// </summary>
        /// <param name="ctrl"></param>
        /// <param name="desControl"></param>
        /// <returns></returns>
        public static int GetHorizontalMiddle(this Control ctrl, Control desControl)
        {
            if (ctrl == null)
            {
                return 0;
            }

            int temp = 0;

            if (ctrl.Width > desControl.Width)
            {
                temp = 0;
            }
            else
            {
                temp = desControl.ClientSize.Width / 2 - ctrl.Size.Width / 2;
            }

            //MessageBox.Show(temp.ToString());

            //如果不是父控件，则需要再加上控件所在位置的left
            if (ctrl.Parent != desControl)
            {
                var f = desControl.FindForm();
                if (f != null)
                {
                    //temp += f.Left + ctrl.Left;

                    Point p = default(Point);

                    desControl.Invoke(() =>
                    {
                        p = desControl.PointToScreen(new Point(0, 0));
                    });

                    temp += p.X;

                }
                //temp += rect.FindForm().Left;
            }

            return temp;

        }
        
        /// <summary>
        ///     将源控件居中对齐到目标控件(垂直于水平居中)
        /// </summary>
        /// <param name="srcControl"></param>
        /// <param name="desControl"></param>
        /// <returns></returns>
        public static Control AlignTo(this Control srcControl, Control desControl)
        {
            if (srcControl==null || desControl==null)
            {
                return srcControl;
            }

            var h = GetHorizontalMiddle(srcControl, desControl);
            var v = GetVerticalMiddle(srcControl, desControl);

            srcControl.Left = h;
            srcControl.Top = v;

            return srcControl;

        }


    }
}
